package com.xish.api.activity;

import com.xish.api.activity.search.ActivitySearchCondition;
import com.xish.api.activity.search.ActivitySearchConditionParser;
import com.xish.spring.security.CheckSignature;
import com.xish.util.MediaType;
import com.xish.view.json.Activity;
import com.xish.view.json.ActivitySearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ActivityController {

    @Autowired
    private ActivitySearchConditionParser parser;

    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = "/activity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
    @CheckSignature
    public Map<String, Object> list(HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();

        ActivitySearchCondition condition = parser.parse(request);
        ActivitySearchResult searchResult = activityService.search(condition);

        data.put("status", "success");
        data.put("result", searchResult);
        return data;
    }

    @RequestMapping(value = "/activity/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
    public Activity findById(@PathVariable("id") int id) {
        return activityService.findActivityById(id);
    }
}
